<?php

namespace App\Http\Controllers\Seller;

use App\Library\KdNiao\KdClient;
use App\Library\KdNiao\Request\KdPrintOrderRequest;
use App\Library\KdNiao\Request\KdQueryRequest;
use App\Models\Express;
use App\Models\KdNiao;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class KuaidiController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->assign(['menu'=>'kuaidi']);
    }

    public function index()
    {

        $expresses = Express::all();

        return $this->view('seller.kuaidi.index', compact('expresses'));
    }

    public function setting(Request $request)
    {

        if ($request->method() == 'POST')
        {
            $ebusinessid = $request->post('ebusinessid');
            $apikey = $request->post('apikey');
            KdNiao::where('uid', $this->uid)->update(compact('ebusinessid', 'apikey'));
            return $this->messager()->render();

        } else {
            $kdniao = KdNiao::where('uid', $this->uid)->firstOrCreate(['uid'=>$this->uid]);
            return $this->view('seller.kuaidi.setting', ['kdniao'=>$kdniao]);
        }
    }

    public function printOrder(Request $request)
    {
        $order_no = $request->input('order_no');

        $kdReq = new KdPrintOrderRequest();
        $kdReq->addOrder($order_no, '默认打印机');

        $client = new KdClient();
        return $client->sendPrintOrderRequest($kdReq);
    }
}
